#include "message.hpp"
#include "net.hpp"

int main()
{
    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MType::REQ_RPC);
    // myrpc::RpcRequest::ptr rrp = std::dynamic_pointer_cast<myrpc::RpcRequest>(bmp);
    // myrpc::RpcRequest::ptr rrp = myrpc::MessageFactory::create<myrpc::RpcRequest>();
   
    // Json::Value param;
    // param["num1"] = 11;
    // param["num2"] = 22;
    
    // rrp->setMethod("Add");
    // rrp->setParams(param);
    // std::string str = rrp->serialize();
    // std::cout << str << std::endl;

    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MType::REQ_RPC);
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // myrpc::RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<myrpc::RpcRequest>(bmp);
    // std::cout << rrp2->method() << std::endl;
    // std::cout << rrp2->params()["num1"].asInt() << std::endl; 
    // std::cout << rrp2->params()["num2"].asInt() << std::endl; 


    // myrpc::TopicRequest::ptr trp = myrpc::MessageFactory::create<myrpc::TopicRequest>();
    // trp->setTopicKey("news");
    // trp->setOptype(myrpc::TopicOptype::TOPIC_PUBLISH);
    // trp->setTopicMsg("Hello world");
    // std::string str = trp->serialize();
    // std::cout << str << std::endl;

    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MType::REQ_TOPIC);
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // myrpc::TopicRequest::ptr trp2 = std::dynamic_pointer_cast<myrpc::TopicRequest>(bmp);
    // std::cout << trp2->topicKey() << std::endl;
    // std::cout << (int)trp2->optype() << std::endl;
    // std::cout << trp2->topicMsg() << std::endl;


    // myrpc::ServiceRequest::ptr trp = myrpc::MessageFactory::create<myrpc::ServiceRequest>();
    // trp->setMethod("add");
    // trp->setOptype(myrpc::ServiceOptype::SERVICE_REGISTRY);
    // trp->setHost(myrpc::Address("127.0.0.1",9090));
    // std::string str = trp->serialize();
    // std::cout << str << std::endl;

    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MType::REQ_SERVICE);
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // myrpc::ServiceRequest::ptr trp2 = std::dynamic_pointer_cast<myrpc::ServiceRequest>(bmp);
    // std::cout << trp2->method() << std::endl;
    // std::cout << (int)trp2->optype() << std::endl;
    // std::cout << trp2->host().first << std::endl; 
    // std::cout << trp2->host().second << std::endl; 


    // myrpc::RpcResponse::ptr trp = myrpc::MessageFactory::create<myrpc::RpcResponse>();
    // trp->setRCode(myrpc::RCode::RCODE_OK);
    // Json::Value val;
    // trp->setResult(33);
    // std::string str = trp->serialize();
    // std::cout << str << std::endl;

    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MType::RSP_RPC);
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // myrpc::RpcResponse::ptr trp2 = std::dynamic_pointer_cast<myrpc::RpcResponse>(bmp);
    // std::cout << (int)trp2->rcode() << std::endl;
    // std::cout << trp2->result() << std::endl;


    // myrpc::TopicResponse::ptr trp = myrpc::MessageFactory::create<myrpc::TopicResponse>();
    // trp->setRCode(myrpc::RCode::RCODE_OK);
    // std::string str = trp->serialize();
    // std::cout << str << std::endl;

    // myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MType::RSP_TOPIC);
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret = bmp->check();
    // if(ret == false)
    // {
    //     return -1;
    // }
    // myrpc::TopicResponse::ptr trp2 = std::dynamic_pointer_cast<myrpc::TopicResponse>(bmp);
    // std::cout << (int)trp2->rcode() << std::endl;


    myrpc::ServiceResponse::ptr trp = myrpc::MessageFactory::create<myrpc::ServiceResponse>();
    trp->setRCode(myrpc::RCode::RCODE_OK);
    trp->setMethod("Add");
    trp->setOptype(myrpc::ServiceOptype::SERVICE_DISCOVERY);
    std::vector<myrpc::Address> addrs;
    addrs.push_back(myrpc::Address("127.0.0.1",9090));
    addrs.push_back(myrpc::Address("127.0.0.2",9091));
    trp->setHost(addrs);
    std::string str = trp->serialize();
    std::cout << str << std::endl;

    myrpc::BaseMessage::ptr bmp = myrpc::MessageFactory::create(myrpc::MType::RSP_SERVICE);
    bool ret = bmp->unserialize(str);
    if(ret == false)
    {
        return -1;
    }
    ret = bmp->check();
    if(ret == false)
    {
        return -1;
    }
    myrpc::ServiceResponse::ptr trp2 = std::dynamic_pointer_cast<myrpc::ServiceResponse>(bmp);
    std::cout << (int)trp2->rcode() << std::endl;
    std::cout << (int)trp2->optype() << std::endl;
    std::cout << trp2->method() << std::endl;
    std::vector<myrpc::Address> addrs1 = trp2->hosts();
    for(auto &addr : addrs1)
        std::cout << addr.first << std::endl << addr.second << std::endl;


    return 0;
}